package com.doyens.gmall.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.doyens.gmall.model.user.UserAddress;
import com.doyens.gmall.model.user.UserInfo;
import com.doyens.gmall.user.mapper.UserAddressMapper;
import com.doyens.gmall.user.mapper.UserMapper;
import com.doyens.gmall.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Override
    public UserInfo login(UserInfo userInfo) {
        //加密密码
        String md5DigestAsHex = DigestUtils.md5DigestAsHex(userInfo.getPasswd().getBytes());
        //根据账号和密码查询
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_name", userInfo.getLoginName()).eq("passwd", md5DigestAsHex);
        UserInfo one = userMapper.selectOne(queryWrapper);
        return one;
    }

    @Override
    public List<UserAddress> findUserAddressListByUserId(String userId) {
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        return userAddressMapper.selectList(queryWrapper);
    }
}
